/**
 * Copyright (c) 2010, Casey Lai
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * 
 * Neither the name of the Cheetah Group nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

package org.cheetahworkflow.designer.ui.xmleditor;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.ui.texteditor.MarkerUtilities;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XMLErrorHandler implements ErrorHandler {
	
	private IResource resource;
	
	private IDocument document;
	
	public XMLErrorHandler(IResource resource, IDocument document) {
		this.resource = resource;
		this.document = document;
	}
	
	public void clearExistingMarkers() {
		try {
			resource.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}
	
	private Map<String, Object> createMap(SAXParseException e) {
		Map<String, Object> map = new HashMap<String, Object>();
		int lineNumber = e.getLineNumber() - 1;
		if (lineNumber < 0) {
			lineNumber = 0;
		}
		int columnNumber = e.getColumnNumber() - 1;
		
		MarkerUtilities.setLineNumber(map, lineNumber);
		MarkerUtilities.setMessage(map, e.getMessage());
		map.put(IMarker.MESSAGE, e.getMessage());
		map.put(IMarker.LOCATION, resource.getFullPath().toString());

		try {
			int start = document.getLineOffset(lineNumber);
			int length = document.getLineLength(lineNumber);
			int end = start + columnNumber;
			if (end > length) {
				ITypedRegion region = document.getPartition(end - 2);
				start = region.getOffset();
			}
			map.put(IMarker.CHAR_START, start);
			map.put(IMarker.CHAR_END, end);
		} catch (BadLocationException ee) {
			ee.printStackTrace();
		}
		
		return map;
	}
	
	private void createMarker(Map<String, Object> map) {
		try {
			IMarker marker = resource.createMarker(IMarker.PROBLEM);
			marker.setAttributes(map);
		} catch (CoreException e1) {
			e1.printStackTrace();
		}
	}

	@Override
	public void error(SAXParseException e) throws SAXException {
		Map<String, Object> map = createMap(e);
		map.put(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
		createMarker(map);
	}

	@Override
	public void fatalError(SAXParseException e) throws SAXException {
		error(e);
	}

	@Override
	public void warning(SAXParseException e) throws SAXException {
		Map<String, Object> map = createMap(e);
		map.put(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
		createMarker(map);
	}
}
